package com.cd.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.cd.bean.User;
import com.cd.service.user.UserService;
import com.cd.utils.SessionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 管理员(用户)
 *@Author zhk
 *@Date 2018-1-13
 **/
@RequestMapping("/user")
@Controller
public class UserController {

	@Autowired
	UserService userService;
	
	/**
	 * 去添加页面
	 * @param id
	 * @param name
	 * @param password
	 * @param role
	 * @return
	 */
	@RequestMapping("/goAdd")
	public String addUser(){
		return "admin/yhgl/user_add";
	}
	
	/**
	 * 添加
	 * @param name
	 * @param password
	 * @param content
	 * @param email
	 * @param phone
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public ModelAndView addUser(@RequestParam(value = "USERNAME")String username,
			@RequestParam(value = "PASSWORD")String password,@RequestParam(value ="CONTENT")String content,
			@RequestParam(value ="EMAIL")String email,@RequestParam(value ="PHONE")String phone) throws Exception{
		User user=new User();
		ModelAndView mv=new ModelAndView();
		user.setUSERNAME(username);
		user.setPASSWORD(password);
		user.setCONTENT(content);
		user.setEMAIL(email);
		user.setPHONE(phone);
		user.setMODIFY_DATE(new Date());
		userService.save(user);
		mv.setViewName("redirect:list.do");
		return  mv;
	}
	/**
	 * 
	 * 删除
	 * @param id
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public ModelAndView deleteUserById(@RequestParam(value = "id")Integer id) throws Exception{
		User user=userService.findById(id);
		ModelAndView mv=new ModelAndView();
		userService.delete(id);
		mv.setViewName("redirect:list.do");
		return mv;
	}
	
	
	
	/**
	 * 修改
	 * @param id
	 * @param username
	 * @param content
	 * @param email
	 * @param phone
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/edit")
	public ModelAndView eidt(@RequestParam(value="USER_ID")Integer id,@RequestParam(value="USERNAME")String username,
			                 @RequestParam(value="CONTENT")String content,@RequestParam(value="EMAIL")String email,
			                 @RequestParam(value="PHONE")String phone) throws Exception {
			ModelAndView mv = new ModelAndView();
			User user = userService.findById(id);
			user.setUSERNAME(username);
			user.setPHONE(phone);
			user.setCONTENT(content);
			user.setEMAIL(email);
			user.setMODIFY_DATE(new Date());
			userService.edit(user);
			mv.setViewName("redirect:list.do");
		return mv;
	}
	
	/**
	 * 去编辑界面
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/goEdit")
	public ModelAndView goEdit(@RequestParam(value="id")Integer id) throws Exception {
		ModelAndView mv = new ModelAndView();
		User user = userService.findById(id);
		mv.addObject("user", user);
		mv.setViewName("admin/yhgl/user_edit");
		return mv;
	}
	/**
	 * 查询（带分页）
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public ModelAndView list(@RequestParam(value = "pn",defaultValue="1")Integer pn) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageHelper.startPage(pn, 15);//开始分页,每页15条记录
	    List<User> list = userService.listAll();
		PageInfo page = new PageInfo(list,5);//设置分页信息
		mv.addObject("pageInfo", page);
		mv.setViewName("admin/yhgl/user_list");
		return mv;
	}
	/**
	 * 去修改密码页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/goUpdatePwd")
	public String updatePwd() throws Exception {
			
		return "admin/yhgl/updatePwd";
	}
	/**
	 * 更新密码
	 * @param session
	 * @param password
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updatePassword")
	public ModelAndView updatePassword(@RequestParam(value="new-password2")String password) throws Exception {
		ModelAndView mv = new ModelAndView();
		User user = (User) SessionUtil.getSession().getAttribute("user");
		user = userService.findById(user.getUSER_ID());
		user.setPASSWORD(password);
		userService.updatePasswordById(user);
		mv.setViewName("admin/yhgl/success");
		return mv;
	}
	/**
	 * 去个人信息页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/goInfo")
	public ModelAndView userInfo() throws Exception {
		ModelAndView mv = new ModelAndView();
		User user = (User) SessionUtil.getSession().getAttribute("user");
		System.out.println(user.getUSERNAME());
		System.out.println(user.getMODIFY_DATE());
		System.out.println(user.getUSER_ID());
		mv.addObject("user", user);
		mv.setViewName("admin/yhgl/user_info");
		return mv;
	}
	/**
	 * 
	 * 批量删除
	 * @param request
	 * @param response
	 */
	@RequestMapping("/del")  
	public void batchDeletes(HttpServletRequest request, HttpServletResponse response) {  
	    String items = request.getParameter("delitems");
	    System.out.println(items);  
	    String[] strs = items.split(",");  
	  
	    for (int i = 0; i < strs.length; i++) {  
	        try {  
	            int a = Integer.parseInt(strs[i]);  
	            userService.delete(a);  
	        } catch (Exception e) {  
	        }  
	    }  
	} 
}
